﻿using System;
using System.Collections.Generic;
using XNAFight.Scene.Core;
using XNAFight.Engine.Inputs;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAFight.Scene.CharacterSelection
{
    class CharacterSelect : Screen
    {
        #region CHAMPS

        protected int nb_players_team_1;
        protected int nb_players_team_2;
        protected List<int> nb_characters_team_1;
        protected List<int> nb_characters_team_2;

        protected List<SingleControlPicker> control_pickers;
        protected CharacterPicker character_picker;
        #endregion

        #region INITIALISATION

        private static List<int> getListWith(int _nb_player, int _nb_char)
        {
            List<int> new_list = new List<int>();
            for (int i = 0; i < _nb_player; i++)
            {
                new_list.Add(_nb_char);
            }
            return new_list;
        }

        public CharacterSelect(int _nb_players_team_1 = 1, int _nb_players_team_2 = 1, int _nb_characters_per_player = 1)
            : this(getListWith(_nb_players_team_1, _nb_characters_per_player), getListWith(_nb_players_team_2, _nb_characters_per_player))
        {
        }


        public CharacterSelect(List<int> _nb_characters_team_1, List<int> _nb_characters_team_2)
            : base()
        {
            nb_players_team_1 = _nb_characters_team_1.Count;
            nb_players_team_2 = _nb_characters_team_2.Count;
            nb_characters_team_1 = _nb_characters_team_1;
            nb_characters_team_2 = _nb_characters_team_2;

            #region CONTROL PICKER INIT & EVENTS

            control_pickers = new List<SingleControlPicker>();
            for (int i = 0; i < nb_players_team_1; i++)
            {
                SingleControlPicker new_control_picker = new SingleControlPicker(CommonContent.CurrentGame, i + 1);
                control_pickers.Add(new_control_picker);
            }
            for (int i = 0; i < nb_players_team_2; i++)
            {
                SingleControlPicker new_control_picker = new SingleControlPicker(CommonContent.CurrentGame, nb_players_team_1 + i + 1);
                control_pickers.Add(new_control_picker);
            }

            for (int i = 0; i < control_pickers.Count; i++)
            {
                control_pickers[i].Cancelling += this.Cancel;
                control_pickers[i].Completed += control_pickers[i].Disable;

                if (i == 0)
                    this.Added += control_pickers[i].Activate;

                control_pickers[i].Completed += controlPickerComplete;
            }

            #endregion

            #region CHARACTER PICKER INIT & EVENTS

            character_picker = new CharacterPicker(nb_characters_team_1, nb_characters_team_2);

            character_picker.CancellingBy += characterPickerCancelled;

            #endregion

            this.Added += character_picker.Add;
            this.Added += character_picker.Activate;
            this.Removing += character_picker.Cancel;
            for (int i = 0; i < control_pickers.Count; i++)
            {
                this.Added += control_pickers[i].Add;
                this.Removing += control_pickers[i].Cancel;
            }

            this.Adding += this.Reset;
        }

        #endregion

        #region OVERRIDE

        public override void HandleInputs()
        {

        }

        public override void Reset()
        {
            base.Reset();

            for (int i = 0; i < control_pickers.Count; i++)
            {
                control_pickers[i].Reset();
            }
            character_picker.Reset();
        }

        public override void Update(GameTime _game_time)
        {
            base.Update(_game_time);

            if (character_picker.SelectionComplete)
                Complete();
        }

        public override void Draw(GameTime _gameTime)
        {
            base.Draw(_gameTime);

            GraphicsDevice.Clear(Color.White);

            CommonContent.SpriteBatch.Begin();

            #region TEAM 1
            for (int i = 0; i < nb_players_team_1; i++)
            {
                WriteLine("Player " + control_pickers[i].PlayerID, CommonContent.SmallFont);
                if (!control_pickers[i].IsControlerOk)
                    WriteLine("Press start", CommonContent.SmallFont);
                else
                {
                    WriteLine(control_pickers[i].Controler.ToString(), CommonContent.SmallFont);
                    for (int j = 0; j < character_picker.persos_team_1[control_pickers[i].PlayerID - 1].Count; j++)
                    {
                        WriteLine(character_picker.persos_team_1[control_pickers[i].PlayerID - 1][j], CommonContent.SmallFont);
                    }
                }
                WriteLine(" ", CommonContent.SmallFont);

            }
            #endregion

            #region TEAM 2
            for (int i = nb_players_team_1; i < nb_players_team_1 + nb_players_team_2; i++)
            {
                WriteLine("Player " + control_pickers[i].PlayerID, CommonContent.SmallFont, true);
                if (!control_pickers[i].IsControlerOk)
                    WriteLine("Press start / enter", CommonContent.SmallFont, true);
                else
                {
                    WriteLine(control_pickers[i].Controler.ToString(), CommonContent.SmallFont, true);
                    for (int j = 0; j < character_picker.persos_team_2[control_pickers[i].PlayerID - 1 - nb_players_team_1].Count; j++)
                    {
                        WriteLine(character_picker.persos_team_2[control_pickers[i].PlayerID - 1 - nb_players_team_1][j], CommonContent.SmallFont, true);
                    }
                }
                WriteLine(" ", CommonContent.SmallFont, true);
            }
            #endregion

            CommonContent.SpriteBatch.End();
        }

        #endregion

        #region METHODES

        private void controlPickerComplete(object sender, EventArgs args)
        {
            for (int i = 0; i < control_pickers.Count; i++)
            {
                control_pickers[i].forbidControler(((SingleControlPicker)sender).Controler);
            }

            activateFirstAvailableControlpicker();

            character_picker.AddControler(((SingleControlPicker)sender).Controler, ((SingleControlPicker)sender).PlayerID);
        }

        private void characterPickerCancelled(object sender, EventArgs<ControlerSelection> args)
        {
            if (character_picker.playerHasACharacter(args.Value.PlayerID))
            {
                character_picker.removeCharacterForPlayer(args.Value.PlayerID);
                return;
            }
            
            for (int i = 0; i < control_pickers.Count; i++)
            {
                control_pickers[i].allowControler(args.Value.Controler);
                control_pickers[i].Disable();
            }

            int j = 0;
            while (j < control_pickers.Count && control_pickers[j].Controler != args.Value.Controler)
            {
                j++;
            }

            if (j < control_pickers.Count)
                control_pickers[j].IsControlerOk = false;

            activateFirstAvailableControlpicker();

            character_picker.RemoveControler(args.Value);
        }

        private void activateFirstAvailableControlpicker()
        {
            int i = 0;
            while (i < control_pickers.Count && control_pickers[i].IsControlerOk)
            {
                i++;
            }

            if (i < control_pickers.Count)
                control_pickers[i].Activate();
        }

        public List<List<string>> getTeam1Characters()
        {
            return this.character_picker.persos_team_1;
        }
        public List<List<string>> getTeam2Characters()
        {
            return this.character_picker.persos_team_2;
        }

        public List<Controler> getTeam1Controlers()
        {
            List<Controler> controlers = new List<Controler>();
            for (int i = 0; i < control_pickers.Count - nb_players_team_2; i++)
            {
                controlers.Add(control_pickers[i].Controler);
            }
            return controlers;
        }
        public List<Controler> getTeam2Controlers()
        {
            List<Controler> controlers = new List<Controler>();
            for (int i = nb_players_team_1; i < control_pickers.Count; i++)
            {
                controlers.Add(control_pickers[i].Controler);
            }
            return controlers;
        }

        #endregion
    }
}
